const app = getApp();
import { averageUser, dealers } from '../../utils/tabBarUrl'
var util = require('../../utils/util.js');
Page({
  /**
   * 页面的初始数据
   */
  data: {
    CustomBar:app.globalData.CustomBar,
    loginInfo: {},
    isopen_bloc:false,  //打开集团码弹窗
    isbloc:0,  //集团客户弹窗 0弹窗关闭，1集团不存在，2集团存在
    account_number: ''//集团码
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    var that = this;
    if (app.globalData.test==0){
      wx.login({
        success: function (res) {
          app.AppHttp("api/Client/getClientOpenId", {
            appId: app.globalData.appId,
            secret: app.globalData.secret,
            code: res.code
          }, function (res) {
            console.log(JSON.stringify(res.data));
            if (res.data.unionid != undefined) {
              that.setData({
                isunionid: res.data.unionid
              });
              app.globalData.unionid = res.data.unionid;
            } else {
              that.setData({
                isunionid: "emp"
              });
            }
            app.AppHttp("api/Client/whetherAuth", { openid: app.globalData.openid, unionid: res.data.unionid },
              function (res) { })
          });
        }
      });
    };
    that.basicInfo_fun();
  },
  basicInfo_fun(){
    var that = this;
    app.AppHttp('api/Client/basicInfo', {
      openid: app.globalData.openid
    }, function (res) {
      if(res.data.phone.length == 11 && res.data.client_phone==res.data.phone && res.data.is_manage==1){  //对比电话和管理员身份
        util.again_login();
      };
      app.globalData.loginInfo = res.data;
      console.log(JSON.stringify(res.data));
      that.setData({
        loginInfo: res.data,
        phone: app.globalData.loginInfo.phone,
        tel: app.globalData.loginInfo.phone.substr(0, 3) + '****' + app.globalData.loginInfo.phone.substr(7),
        test: app.globalData.test,
        client_is_store:res.data.client_is_store,
        type_level:res.data.type_level,
        good_dir_set:res.data.good_dir_set,  //2:总部目录
      });
      app.globalData.set_receved=res.data.set_receved;  //是否设置收货 0否1是
      app.globalData.type_level = res.data.type_level;  
      if(res.data.client_is_store<0 && res.data.type_level==0 || res.data.client_is_store==0){        
        app.globalData.list =  averageUser; 
        if (typeof that.getTabBar === 'function' && that.getTabBar()) {
          that.getTabBar().setData({
            selected: 2,
            list: app.globalData.list
          })
        };     
      }else if(res.data.client_is_store<0 && res.data.type_level==1 || res.data.client_is_store==1){
          app.globalData.list =  dealers;
          if (typeof that.getTabBar === 'function' && that.getTabBar()) {
            that.getTabBar().setData({
              client_is_store:res.data.client_is_store,
              type_level:res.data.type_level,
              is_manage:res.data.is_manage,  //是否为管理员
              ischarge:res.data.ischarge,  //库管 是1 否0
              is_purchase_check:res.data.is_purchase_check,  //采购单审核  是1 否0
              selected:4,
              list: app.globalData.list
            })
          }; 
      };    
    })
  },
  // 去审批管理
  tocheck_order:function(){
    app.tocheck_manage(this.data.check_order_ids,this.data.purchase_order_ids,this.data.receipt_order_ids);
  },
  // 回收站(查看取消订单)
  cancel_order:function(){
    wx.navigateTo({
      url: '/trash/pages/cancel_order/cancel_order'
    });
  },
  // 去基本设置页面
  tojibeshezhi:function(){
    if (this.data.loginInfo.is_manage == 2 && this.data.test == 0){
      wx.navigateTo({
        url: '/packageA/pages/jibenshezhi/jibenshezhi'
      });
    }else{
      wx.showLoading({
        title: '暂无权限',
        duration: 500
      });
    }
  },
  // 去个人设置页面
  toperson:function(){
    if (this.data.test == 0) {
      wx.navigateTo({
        url: '/pages/wo/gerenshezhi/gerenshezhi?is_manage='+this.data.loginInfo.is_manage
      })
    } else if (this.data.test == 1){   //体验者不能进入个人设置修改个人信息
      wx.showLoading({
        title: '暂无权限',
        duration: 500
      })
    }
  },
  // 去员工管理页面
  toGuanli:function(){
    wx.navigateTo({
      url: '/pages/wo/jigouquanxian/guanlichufang/yuangongguanli/yuangongguanli'
    });
  },
  // 去供应商列表页面
  toSuper:function(){
    wx.navigateTo({
      url: '/super_space/pages/supper/supper'
    });
  },
  // 前往对账
  toduizhang_zi:function(){
    var val={};
    var loginInfo=this.data.loginInfo;
    val.logo=loginInfo.logo && loginInfo.logo!=0?loginInfo.logo:'http://www.xiaobangcai.com/upload/smallprogram/super_def.png';
    val.company_name=loginInfo.client_abbre;
    val.real_name=loginInfo.real_name!= null?loginInfo.real_name:loginInfo.nickname;
    console.log(loginInfo);
    val.phone=loginInfo.phone;
    val.status=1; 
    wx.navigateTo({
      url: '/super_space/pages/supper/supperguanli/supperguanli?id='+'-10010'+"&val="+JSON.stringify(val)+'&is_zi_cai='+1
    });  
  },
  // 商品管理
  toshangpinguan:function(){
    wx.navigateTo({
      url: '/pages/wo/shangpinguanli/shangpinguanli'
    })
  },
  // 打开集团码弹窗
  bloc_but:function(){
    this.setData({
      isopen_bloc:true,
      account_number:''
    });
  },
  // 输入集团码
  blocInput:function(e){
    this.setData({
      account_number:e.detail.value
    })
  },
  // 取消启用
  cal_apply_bloc:function(){
    this.setData({
      isopen_bloc:false
    });
  },
  // 点击启用
  apply_bloc:function(){
    var that = this;
    if (that.data.account_number == '') {
      wx.showLoading({
        title: '请输入集团码',
        duration: 500
      });
      return;
    };
    app.AppHttp('api/Supplier/getGroupInfoAll', {
      account_number: that.data.account_number,    
    }, function (res) {
      console.log(JSON.stringify(res.data));
      if (res.data.status == 1){  //集团存在
        that.setData({
          isopen_bloc:false,
          isbloc:2,
          bloc_kehu:res.data.data
        });
      }else{  //集团不存在
        that.setData({
          isopen_bloc:false,
          isbloc:1
        });
      }
    });
  },
  // 关闭集团客户弹窗
  confirm_bloc_kehu:function(){
    this.setData({
      isbloc:0
    });
  },
  // 确定并关闭集团码弹窗
  confirm_bloc:function(){
    var that = this;
    if (that.data.account_number==''){
      wx.showLoading({
        title: '请输入集团码',
        duration:500
      });
      return;
    }; 
    app.AppHttp('api/Client/setClientBelongGroup', {
      id: that.data.loginInfo.id,
      account_number: that.data.account_number
    }, function (res) {
      console.log(JSON.stringify(res.data));
      if(res.data.status==1){
        wx.showToast({
          title: "设置成功"
        }); 
        that.setData({
          isbloc: false 
        });
        that.basicInfo_fun();     
      }else{
        wx.showToast({
          title: res.data.msg
        }); 
      }
    });
  },
  // 关注公众号
  focus_on:function(){
    wx.navigateTo({
      url: '/packageA/pages/focus/focus'
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },
  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    var that = this;
    that.setData({
      client_is_store:app.globalData.loginInfo.client_is_store,
      type_level:app.globalData.type_level,
    });
    if(app.globalData.loginInfo.client_is_store<0 && app.globalData.type_level==0 || app.globalData.loginInfo.client_is_store==0){
      app.globalData.list =  averageUser;
      if (typeof this.getTabBar === 'function' && this.getTabBar()) {
        this.getTabBar().setData({
          selected: 2,
          list: app.globalData.list
        })
      };
    }else if(app.globalData.loginInfo.client_is_store<0 && app.globalData.type_level==1 || app.globalData.loginInfo.client_is_store==1){  
        app.globalData.list =  dealers;
        if (typeof this.getTabBar === 'function' && this.getTabBar()) {
          this.getTabBar().setData({
            client_is_store:app.globalData.loginInfo.client_is_store,
            type_level:app.globalData.type_level,
            is_manage:app.globalData.loginInfo.is_manage,  //是否为管理员
            ischarge:app.globalData.loginInfo.ischarge,  //库管 是1 否0
            is_purchase_check:app.globalData.loginInfo.is_purchase_check,  //采购单审核  是1 否0
            selected: 4,
            list: app.globalData.list
          })
        };  
    };      
    wx.hideShareMenu();//隐藏转发分享按钮
    app.globalData.caigou_shua=1;
    app.globalData.dingdan_show = 1; //切换导航时，返回订单页需要刷新
    if (app.globalData.loginInfo != null) {
      that.setData({
        loginInfo: app.globalData.loginInfo
      });
    };
    app.get_count_list(that);
    that.onLoad();
  },
  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    if (app.globalData.loginInfo != null) {
      this.setData({
        loginInfo: app.globalData.loginInfo
      });
    }
    this.onLoad();
    wx.stopPullDownRefresh();
  },
  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    return {
      title: '小帮菜下单端',
      path: 'pages/shangpin/shangpin',
      imageUrl:'http://www.xiaobangcai.com/upload/smallprogram/mall_card1.jpg'
    }
  },
  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  }
})